package hp.com.service.impl;

import hp.com.dao.BaseDao;
import hp.com.dao.ProductDAO;
import hp.com.dao.SukPropDao;
import hp.com.dao.impl.ProductDAOImpl;
import hp.com.dao.impl.SukPropdaoImpl;
import hp.com.entity.PageInfo;
import hp.com.entity.Product;
import hp.com.entity.Sells;
import hp.com.entity.Suk_Options;
import hp.com.service.ProductService;
import hp.com.util.GeneralUtil;

import java.util.List;
import java.util.Random;

public class ProductServiceImpl implements ProductService {
    ProductDAO pd = new ProductDAOImpl();

    /**
     * 通过ID获取Product商品对象
     * @param id 商品ID
     * @return
     * @throws Exception
     */
    @Override
    public Product GetProductById(int id) throws Exception {
        return pd.GetProductById(id);
    }

    /**
     * 分类查询Sells对象
     * @param page
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<Sells> listPage(PageInfo<Sells> page) throws Exception {
        PageInfo<Sells> pageInfo = pd.listPage(page);
        for (int i = 0; i < pageInfo.getPageList().size(); i++) {
            String sp = pageInfo.getPageList().get(i).getDescription();
            String picName = pageInfo.getPageList().get(i).getPicName();
            String[] indexp = picName.split(",");
            String[] index = sp.split(" ");
            String sub = indexp[0];
            String s = index[0];
            pageInfo.getPageList().get(i).setDescription(s);
            pageInfo.getPageList().get(i).setPicName(sub);
        }
        return pageInfo;
    }

    /**
     * 通过分类获取Page分页对象
     * @param page 分页对象
     * @param category 种类
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<Sells> listPageByGroy(PageInfo<Sells> page,int category) throws Exception {
        PageInfo<Sells> pageInfo = pd.listPageByGory(page, category);
        for (int i = 0; i < pageInfo.getPageList().size(); i++) {
            String sp = pageInfo.getPageList().get(i).getDescription();
            String picName = pageInfo.getPageList().get(i).getPicName();
            String[] indexp = picName.split(",");
            String[] index = sp.split(" ");
            String sub = indexp[0];
            String s = index[0];
            pageInfo.getPageList().get(i).setDescription(s);
            pageInfo.getPageList().get(i).setPicName(sub);
        }
        return pageInfo;
    }
    /**
     * 通过商品ID获取Suk选项对象
     * @param id 商品ID
     * @return
     * @throws Exception
     */
    @Override
    public List<Suk_Options> getSUKOptionsByGoodId(int id) throws Exception {
        SukPropDao spd = new SukPropdaoImpl();
        return spd.getSUKOptionsByGoodId(id);
    }

    /**
     * 获取suk价格
     * @param suk suk搭配信息
     * @return 价格
     * @throws Exception
     */
    @Override
    public double GetSukPrice(String suk) throws Exception {
        SukPropDao spd = new SukPropdaoImpl();
        return spd.GetSukPrice(suk);
    }

    /**
     * 获取一个商品的最高价格和最低价格
     * @param id ID
     * @return 最高/低价格，字符串格式： ¥xx～¥xx
     * @throws Exception
     */
    @Override
    public String GetLowestAndHighestPrice(int id) throws Exception {
        return pd.GetLowestAndHighestPrice(id);
    }

    /**
     * 获取指定个数的产品作为推荐
     * @param number 获取数量
     */
    @Override
    public List<Sells> getRecommendation(int number) throws Exception {
        Random random = new Random();
        //创建输入DAO的id数组
        int[] ids = new int[number];
        int index = pd.getProductCount();
        //
        for (int i = 0; i < number; i++) {
            int newNum = random.nextInt(index);
            while(!GeneralUtil.checkRepetition(ids,newNum)){
                newNum = random.nextInt(index);
            }
            ids[i]=newNum;
        }
        return pd.getSellById(ids);
    }


    @Override
    public List<Sells> getRecommendation(String cate,int number) throws Exception {
        Random random = new Random();
        //获取当前分类的所有ID
        int[] ids = new int[number];
        List<Integer> totalId = pd.getCategoryIds(cate);
        for (int i = 0; i < number; i++) {
            int newNum = totalId.get(random.nextInt(totalId.size()));
            while(!GeneralUtil.checkRepetition(ids,newNum)){
                newNum = totalId.get(random.nextInt(totalId.size()));
            }
            ids[i]=newNum;
        }
        return pd.getSellById(ids);
    }
}
